package com.webbei.product.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;

import com.webbei.custom.service.CustomSer;
import com.webbei.pojo.PicInfor;
import com.webbei.product.service.ProductService;
import com.webbei.util.Arithmetic;
import com.webbei.util.Thumbnail;

public class ShowOtherProducts {
	private String mark;
    private ProductService ps; 
  //  private List<OtherProduct> otherProducts;
    private CustomSer cs;
    private String autoUrl;
    private List<Product>products;
    private String widthStr;
    private String heightStr;
	public String execute(){
		
		System.out.println("showOtherProducts...."+widthStr+","+heightStr);
		Arithmetic.getKey("webbei");
		double h = Double.parseDouble(heightStr.substring(0, heightStr.lastIndexOf("px")));
		double w = Double.parseDouble(widthStr.substring(0, widthStr.lastIndexOf("px")));
		double p = w/h;
		System.out.println("P:"+p);
		products = ps.getProducts();
		String real = ServletActionContext.getServletContext().getRealPath("/");
		for(int i=0;i<products.size();i++){
			String bgPic = products.get(i).getUrl();
			String auto = products.get(i).getAutoLogos();
			if(auto!=null){
				JSONObject json = JSONObject.fromObject(products.get(i).getAutoLogos());
				int left = (Integer) json.get("left");
				int top = (Integer)json.get("top");
				int height = (Integer)json.get("height");
				int width = (Integer)json.get("width");
				System.out.println("left....."+left+autoUrl);
				System.out.println(products.get(i).getUrl());
				List<PicInfor> pics = new ArrayList<PicInfor>();  
				PicInfor pic = new PicInfor();
				pic.setPicUrl(autoUrl);
				pic.setLeft(left);
				pic.setTop(top);
				pic.setHeight(height);
				pic.setWidth((int) (height*p));
				pics.add(pic);
				products.get(i).setPi(pic);
				String pUrl = products.get(i).getUrl();
				String url = real+cs.drawPic(pUrl, pics);
				products.get(i).setUrl(Arithmetic.getEncString(pUrl));
				try {
					String newUrl = Thumbnail.saveImageAsJpg(url, 300);
					products.get(i).setNewUrl(newUrl);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}	
		}
		return "success";
	}
	
	public String getMark() {
		return mark;
	}

	public void setMark(String mark) {
		this.mark = mark;
	}

	public void setPs(ProductService ps) {
		this.ps = ps;
	}

//	public List<OtherProduct> getOtherProducts() {
//		return otherProducts;
//	}
//
//	public void setOtherProducts(List<OtherProduct> otherProducts) {
//		this.otherProducts = otherProducts;
//	}



	public void setCs(CustomSer cs) {
		this.cs = cs;
	}

	public String getAutoUrl() {
		return autoUrl;
	}

	public void setAutoUrl(String autoUrl) {
		this.autoUrl = autoUrl;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public String getWidthStr() {
		return widthStr;
	}

	public void setWidthStr(String widthStr) {
		this.widthStr = widthStr;
	}

	public String getHeightStr() {
		return heightStr;
	}

	public void setHeightStr(String heightStr) {
		this.heightStr = heightStr;
	}
	
}
